<script setup>
import { reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router';
import * as Api from '@/api'
import TreeMenu from './TreeMenu.vue'
import BreadCrumb from './BreadCrumb.vue'


const router = useRouter();
const store = useStore()
const state = reactive({
  collapse: false,
  noticeCount: 0,
  userMenu: [],
  activeMenu: location.hash.slice(1)
})

onMounted(() => {
  getNoticeCount()
  getMenuList()
})

const getNoticeCount = async () => {
  try {
    let res = await Api.getNoticeCount();
    state.noticeCount = res;
  } catch (error) {
    console.error(error)
  }
}

const getMenuList = async () => {
  try {
    let res = await Api.getPermissionList();
    state.userMenu = res.menuList;
    store.commit('saveMenuList', res.menuList)
    store.commit('saveActionList', res.actionList)
  } catch (error) {
    console.error(error)
  }
}

const handelLogout = (command) => {
  console.log(command)
  if (command === 'logout') {
    store.commit('saveUserInfo', '')
    state.userInfo = {}
    router.push('/login')
  }
}

</script>
<template>
  <div class="basic-layout">
    <!-- state.collapse -->
    <div class="nav-side" :style="{ width: state.collapse ? '64px' : '200px' }">
      <!-- logo -->
      <div class="logo">
        <img src="./../assets//logo.png" alt="">
        <span>Manger</span>
      </div>
      <!-- 菜单 -->
      <el-menu :collapse="state.collapse" text-color="#fff" background-color="#001529" router
        :default-active="state.activeMenu" class="nav-menu">
        <TreeMenu :userMenu="state.userMenu" />
      </el-menu>
    </div>
    <div class="content-right" :style="{ 'margin-left': state.collapse ? '64px' : '200px' }">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="state.collapse = !state.collapse">
            <el-icon>
              <Fold v-if="!state.collapse" />
              <Expand v-if="state.collapse" />
            </el-icon>
          </div>
          <div class="bread">
            <BreadCrumb />
          </div>
        </div>
        <div class="user-info">
          <el-badge :is-dot="state.noticeCount !== 0" class="notice" type="danger">
            <el-icon class="icon-bell">
              <Bell style="top: 10px;" />
            </el-icon>
          </el-badge>
          <el-dropdown @command="handelLogout">
            <span class="user-link">
              {{ store.state.userInfo.userName }}
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email">邮箱: {{ store.state.userInfo.userEmail }}</el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
.basic-layout {
  position: relative;

  .nav-side {
    position: fixed;
    width: 200px;
    overflow: hidden;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow-y: auto;
    transition: width 0.5s;

    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;

      img {
        width: 32px;
        height: 32px;
        margin: 0 16px;
      }
    }

    .nav-menu {
      border-right: none;
      height: calc(100vh - 50px);
    }
  }

  .content-right {
    margin-left: 200px;
    transition: margin 0.5s;

    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      border-bottom: 1px solid #ccc;
      justify-content: space-between;
      padding: 0 20px;

      .nav-left {
        display: flex;
        flex-direction: row;

        .menu-fold {
          cursor: pointer;
          padding-right: 16px;
        }

        .bread {
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }

      .user-info {
        .user-link {
          margin-left: 15px;
          line-height: 50px;
        }

        .notice {
          line-height: 30px;
        }
      }
    }

    .wrapper {
      padding: 20px;
      background: #eef0f3;
      height: calc(100vh - 50px);

      .main-page {
        // background: #fff;
        height: 100%;
      }
    }
  }
}
</style>